<script>
import { init } from "@/shopro";

export default {
  data() {
    return {
      options: null,
      isPrivacyPage: false,
    };
  },
  onLaunch(options) {
    this.options = options;
    console.log("====123");
    this.checkPrivacyAgreement();
  },
  watch: {
    $route(to) {
      this.isPrivacyPage = to.path.includes("/public/richtext");
      console.log("===this.isPrivacyPage ");
      // if (!this.isPrivacyPage) {
      // 	this.checkPrivacyAgreement()
      // }
    },
  },
  methods: {
    async checkPrivacyAgreement() {
      const agreed = uni.getStorageSync("privacyAgreed");
      console.log(
        "===隐私协议状态： ",
        uni.getStorageSync("privacyAgreed"),
        agreed
      );
      if (!agreed) {
        try {
          const agreed = await this.$privacy.show(this);
          console.log("===隐私协议结果： ", agreed);
          if (agreed) {
            uni.setStorageSync("privacyAgreed", "true");
            this.initSDKs();
          } else {
            // #ifdef APP-PLUS
            // if (plus.runtime.isAgreePrivacy()) {
            // }
            this.initSDKs();
            // #endif
            // #ifdef H5
            // 用户不同意，显示退出提示
            uni.showModal({
              title: "提示",
              content: "您需要同意隐私协议才能使用本应用，是否退出应用？",
              confirmText: "退出",
              cancelText: "重新查看",
              success: (res) => {
                if (res.confirm) {
                  // #ifdef APP-PLUS
                  plus.runtime.quit();
                  // #endif
                  // #ifdef H5
                  window.close();
                  // #endif
                } else {
                  // 重新检查隐私协议
                  setTimeout(() => {
                    this.checkPrivacyAgreement();
                  }, 500);
                }
              },
            });
            // #endif
          }
        } catch (e) {
          console.error("隐私协议异常:", e);
          // 降级处理：允许继续使用基础功能
          this.initSDKs();
        }
      } else if (agreed === "true") {
        this.initSDKs();
      } else {
        // 用户之前选择了不同意，重新显示隐私协议
        uni.removeStorageSync("privacyAgreed");
        this.checkPrivacyAgreement();
      }
    },
    initSDKs() {
      init(this.options);
    },
  },
};
</script>

<style lang="scss">
@import "static/style/index.scss";
</style>
